﻿Ext.define(Fsl.app.getAbsController('C01I002001'), {
	extend      : 'Ext.app.Controller', 
	requires    : Fsl.app.getMergedRequires({
		apps: [],
		misc: []
	}),  
	views       : Fsl.view.getViews(['CenterPanel','WestPanel', 'V01I002001', 'V01I002001X01:02']),
	refs        : [{
        ref       : 'activeWindow',
        selector  : 'window'
	}],
	init        : function() {        
        this.control({ 
			'v01i002001x01' :{
				itemdblclick : this.preUpdate
			},
			'v01i002001x02 button[action=save]': {
                click        : this.onSave
            },
			'v01i002001x03 button[action=update]': {
				click        : this.onUpdate
			}
        });
    },
	onSave            : function(button){
        var win = button.up('window'),
		form    = win.down('form'),
		record  = form.getRecord(),
		values  = form.getValues(),
		status  = 'win-statusbar-v01i002001',
		statusView  = 'win-statusbar-v01i002002';                    

        if (form.getForm().isValid()) { 
			if (form.getForm().isDirty()) { 
					Fsl.model.save('M01I002001', values, {
						scope   : this,
						success : function(model, operation) {
							form.loadRecord(model);
							Fsl.severity.info(status, operation);
							Fsl.severity.info(statusView, operation);
							Fsl.store.getStore('S01I002001').load();
						},
						failure : function(model, operation){
							Fsl.severity.error(status, operation);
							Fsl.severity.error(statusView, operation);
						}
					});
				} else {
					var message  = 'No change Occured';
					Fsl.severity.warn(status, message);
					Fsl.severity.warn(statusView, message);
			    }
        } else {
            var message  = 'Necessary Field Required';
			Fsl.severity.warn(status, message);
			Fsl.severity.warn(statusView, message);
        }     
    },
	preUpdate         : function(model, records){
        var view = Fsl.app.getWidget('V01I002001X02');
        view.down('form').loadRecord(records);  
		view.down('button[text=Save]').setText('Update');	
    }
});
